/**********************************************************************************************************
*	© 2010 Autodata Solutions Company. All Rights Reserved
*	This source code is the confidential and proprietary information of Autodata Solutions Company.
*	The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code 
*	without the prior written consent of Autodata Solutions Company.
*
*	Author(s)	: Chris Raposo
*	Purpose		: Add page functionality for compare vehicle select
* 	Version		: 1.0
*
* 	Namespace	: 
*********************************************************************************************************/

(function() {
    var EVENTS = net.autodata.EventBus.EVENTS;
    var baseAcode = null;

    function onTrimSelected(acode) {
        baseAcode = acode;
        var ajax = new net.autodata.data.JsonRequest({
            method: 'POST',
            url: String.format('{0}/Ajax/Compare/DefaultCompetitors/', ASC.cfg.getLocaleContextPath())
        });

        ajax.request({
            params: {
                acode: acode
            },
            success: onSuccess,
            failure: onFailed
        });
    }

    function onSuccess(response, ioArgs) {
        var obj = {
            baseAcode: baseAcode,
            competitors: []
        }

        if (response && response.acodes) {
            for (var x = 0; x < response.acodes.length; ++x) {
                var acode = response.acodes[x];
                obj.competitors.push(acode || '');

                if (obj.competitors.length > 2) {
                    break;
                }
            }
        }

        var session = net.autodata.Session.getInstance();
        var url = session.getPageUrls()['results'];
        if (url) {
            var form = ASC.Serializer.objectToForm({
                action: String.format(url, ASC.cfg.getLocaleContextPath()),
                method: 'GET',
                params: obj
            }, true);
            form.submit();
        }
    }

    function onFailed(response, ioArgs) {

        var session = net.autodata.Session.getInstance();
        var url = session.getPageUrls()['error'];
        if (url) {
            var form = ASC.Serializer.objectToForm({
                action: String.format(url, ASC.cfg.getLocaleContextPath()),
                method: 'GET',
                params: response
            }, true);

            form.submit();
        }
    }

    Ext.onReady(function() {
        var session = net.autodata.Session.getInstance();
        var eb = net.autodata.EventBus.getInstance();

        var ptb = new net.autodata.compare.toolbars.PageToolbar({
            viewCfg: { activeKey: 'compareVehicle' },
            shownav: false,
            renderTo: 'comparetoppanel-wrap'
        });

        var ds = net.autodata.modelwalk.data.YearMakeModelDataStore.getInstance();
        ds.load();

        var vsp = new net.autodata.modelwalk.VehicleSelectorPanel({
            renderTo: 'comparevehicleselector-wrap',
            listeners: {
                'trimselected': onTrimSelected,
                scope: this
            },
            viewCfg: {
                title: ASC.message('net.autodata.compare.vehicleselector.title')
            }
        });

        eb.fireEvent(EVENTS.READY);
    });
})();